package com.gs.cd.service.old.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diboot.core.binding.Binder;
import com.gs.cd.api.ApiResult;
import com.gs.cd.api.HttpHeadersParam;
import com.gs.cd.cloud.start.annotation.RequestBodyData;
import com.gs.cd.service.auth.model.AuthUser;
import com.gs.cd.service.auth.model.AuthUserRole;
import com.gs.cd.service.auth.service.AuthUserRoleService;
import com.gs.cd.service.auth.service.AuthUserService;
import com.gs.cd.service.feign.sso.SSOClient;
import com.gs.cd.service.old.controller.model.User;
import com.gs.cd.service.role.model.AuthRole;
import com.gs.cd.service.role.service.AuthRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户
 *
 * @author hippo
 * @since 2020-02-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/auth/user")
public class UserController {

    @Autowired
    AuthUserService userService;

//    @Autowired
//    SSOClient ssoClient;

//
//    /**
//     * sso用户
//     *
//     * @param ssoUserIds
//     * @return
//     */
//    @PostMapping("ssoUser")
//    public ApiResult getUserInfoBySSOUserId(@RequestBody List<String> ssoUserIds) {
//
//        if (null == ssoUserIds || ssoUserIds.isEmpty()) {
//            return ApiResult.error("sso user id 不能为空");
//        }
//        List<User> users;
//        try {
//            users = userService.lambdaQuery()
//                    .select(User::getLoginName
//                            , User::getName
//                            , User::getSsoUserId
//                            , User::getId
//                    )
//                    .in(User::getSsoUserId, ssoUserIds)
//                    .list();
//        } catch (Exception e) {
//            return ApiResult.error("获取SSO用户信息失败");
//        }
//
//        if (null == users || users.isEmpty()) {
//            return ApiResult.error("sso用户Id为:" + String.join(",", ssoUserIds) + " 的用户不存在");
//        } else {
//            return ApiResult.successList(users);
//        }
//
//    }
//
//
//    /**
//     * 新增or修改
//     * 修改 不更新密码
//     *
//     * @param user
//     * @return
//     */
//    @ResponseBody
//    @PostMapping("coru")
//    @OperLog(operDesc = "用户修改密码", operModul = "认证模块", operType = OperTypeEnum.Mod)
//    public ApiResult createOrUpdateRole(@RequestBodyData User user) {
//        userService.createOrUpdateUser(user);
//        return ApiResult.success();
//    }
//
//    /**
//     * 删除
//     *
//     * @param userId
//     * @return
//     */
//    @ResponseBody
//    @DeleteMapping("delete/{userId}")
//    @OperLog(operDesc = "用户删除", operModul = "认证模块", operType = OperTypeEnum.Del)
//    public ApiResult deleteUser(@PathVariable("userId") @Min(value = 1, message = "用户id需大于0") Long userId) {
//        userService.removeById(userId);
//        return ApiResult.success("删除成功");
//    }

    /**
     * 查询列表
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("search/{pageNo}/{pageSize}")
    public ApiResult search(@RequestBodyData User user
            , @PathVariable("pageNo") Integer pageNo
            , @PathVariable("pageSize") Integer pageSize
    ) {
        Page<AuthUser> userPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<AuthUser> lambdaWrapper = new QueryWrapper<AuthUser>().lambda();
        //姓名
        if (StrUtil.isNotBlank(user.getName())) {
            lambdaWrapper.like(AuthUser::getName, user.getName());
        }
        //登录名
        if (StrUtil.isNotBlank(user.getLoginName())) {
            lambdaWrapper.like(AuthUser::getLoginName, user.getLoginName());
        }
        //邮箱
        if (StrUtil.isNotBlank(user.getEmail())) {
            lambdaWrapper.like(AuthUser::getEmail, user.getEmail());
        }
        lambdaWrapper.orderByDesc(AuthUser::getId);
        userPage = (Page<AuthUser>) userService.page(userPage, lambdaWrapper);
//        auth: seven
//
//        date: 2020/12/28 11:11:10
//        此处是否为管理员，应该有角色来判断
//        userPage.getRecords().forEach(u -> u.setIsAdmin(userOwnRoleService.checkUserIsAdmin(u.getLoginName())));
        return ApiResult.success(userPage);
    }

}

